<div class="container-fluid">
    <div class="row flex-xl-nowrap">
        <!-- Sidenav -->
        @Html.Partial("22-sidenav.cshtml")
        @{
            var tocModel = Model.GetToc();
            var content = Html.Partial("24-actualcontent.cshtml");
            static IHtmlContent GetPageToC(ref IHtmlContent html)
            {
                var htmlDocument = new HtmlDocument();
                using var sw = new StringWriter();
                html.WriteTo(sw, HtmlEncoder.Default);
                htmlDocument.LoadHtml(sw.ToString());
                var currentHeaderLevel = 1;
                var started = false;
                var ret = string.Empty;
                foreach (HtmlNode node in htmlDocument.DocumentNode?.SelectNodes("//*[self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or self::h6]") ?? Enumerable.Empty<HtmlNode>())
                {
                    if (!started)
                    {
                        ret += "<ul class=\"section-nav\">";
                        started = true;
                    }
        
                    var thisHeaderLevel = ((int)node.Name[1]) - ((int)'0');
                    IndentIfNeeded(ref currentHeaderLevel, thisHeaderLevel, ref ret);
                    OutdentIfNeeded(ref currentHeaderLevel, thisHeaderLevel, ref ret);
                    ret += $"<li class=\"toc-entry toc-{node.Name}\">";
                    var id = node.GetAttributeValue("id", null);
                    if (id is null)
                    {
                        node.SetAttributeValue("id", id = new string(node.InnerText.ToLower().Select(x => "abcdefghijklmnopqrstuvwxyz0123456789".Contains(x) ? x : '-').ToArray()).Trim('-'));
                    }

                    ret += $"<a href=\"#{id}\">{node.InnerText}</a>";
                    ret += "</li>";
                }
        
                OutdentIfNeeded(ref currentHeaderLevel, 1, ref ret);
                if (started)
                {
                    ret += "</ul>";
                }
        
                html = new HtmlString(htmlDocument.DocumentNode.OuterHtml);
                return new HtmlString(ret);
            }
        
            static void IndentIfNeeded(ref int currentHeaderLevel, int thisHeaderLevel, ref string ret)
            {
                for (; currentHeaderLevel < thisHeaderLevel; currentHeaderLevel++)
                {
                    ret += "<ul>";
                }
            }
        
            static void OutdentIfNeeded(ref int currentHeaderLevel, int thisHeaderLevel, ref string ret)
            {
                for (; currentHeaderLevel > thisHeaderLevel; currentHeaderLevel--)
                {
                    ret += "</ul>";
                }
            }
        }
        
        <!-- Content Navigation-->
        <div class="d-none d-xl-block col-xl-2 ct-toc">
            @GetPageToC(ref content)
        </div>

        <main class="col-12 col-md-9 col-xl-8 py-md-3 pl-md-5 @ContentClasses" role="main">
            @if (!(tocModel?.Node is null))
            {
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        @foreach (var ancestor in tocModel.Node.Ancestors)
                        {
                            <li class="breadcrumb-item"><a href="@InputUrl(ancestor.Href)">@ancestor.Name</a></li>
                        }

                        <li class="breadcrumb-item active" aria-current="page">@tocModel.Node.Name</li>
                        <li class="ml-auto silk-sidebar-activator"><a data-toggle="collapse" href="#ct-docs-nav" role="button" aria-expanded="false" aria-controls="ct-docs-nav"><i class="fa fa-bars"></i></a></li>
                    </ol>
                </nav>
            }

            <!-- Content -->
            @content
            @if ((tocModel?.Root?.Metadata?.ContainsKey("theme.silk.giscus.repo") ?? false) &&
                (tocModel?.Root?.Metadata?.ContainsKey("theme.silk.giscus.repo.id") ?? false) &&
                (tocModel?.Root?.Metadata?.ContainsKey("theme.silk.giscus.category") ?? false) &&
                (tocModel?.Root?.Metadata?.ContainsKey("theme.silk.giscus.category.id") ?? false) &&
                (!tocModel?.Node?.Metadata?.ContainsKey("theme.silk.giscus.exempt") ?? true))
            {
                <hr class="silk-last-hr" />
                <div class="giscus" />
            }
        </main>
    </div>
</div>
